#import "LiveGiftShowView.h"
#import "LiveGiftListModel.h"
#import "UIImageView+WebCache.h"
#import "Masonry.h"
static CGFloat const kNameLabelFont = 12.0;
#define kNameLabelTextColor [UIColor whiteColor]
static CGFloat const kGiftLabelFont = 10.0;
#define kGiftLabelTextColor [UIColor orangeColor]
static CGFloat const kGiftNumberWidth = 15.0;
@interface LiveGiftShowView ()
@property (nonatomic ,weak) UIImageView * backIV;
@property (nonatomic ,weak) UIImageView * iconIV;
@property (nonatomic ,weak) UILabel * nameLabel;
@property (nonatomic ,weak) UILabel * sendLabel;
@property (nonatomic ,weak) UIImageView * giftIV;
@property (nonatomic ,strong) NSTimer * liveTimer;
@property (nonatomic ,assign) NSInteger liveTimerForSecond;
@property (nonatomic ,assign) BOOL isSetNumber;
@end
@implementation LiveGiftShowView
#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:CGRectMake(frame.origin.x, frame.origin.y, kViewWidth, kViewHeight)];
    if (self) {
        self.liveTimerForSecond = 0;
        [self setupContentContraints];
        self.creatDate = [NSDate date];
        self.kTimeOut = 3;
        self.kRemoveAnimationTime = 0.5;
        self.kNumberAnimationTime = 0.25;
    }
    return self;
}
- (instancetype)init{
    self = [super init];
    if (self) {
        self.liveTimerForSecond = 0;
        [self setupContentContraints];
    }
    return self;
}
- (void)setModel:(LiveGiftShowModel *)model{
    if (!model) {
        return;
    }
    _model = model;
    self.nameLabel.text = model.user.name;
    [self.iconIV sd_setImageWithURL:[NSURL URLWithString:model.user.iconUrl] placeholderImage:[UIImage imageNamed:@"LiveDefaultIcon"]];
    self.sendLabel.text = model.giftModel.rewardMsg;
    [self.giftIV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",model.giftModel.picUrl]] placeholderImage:nil];
}
- (void)resetTimeAndNumberFrom:(NSInteger)number{
    self.numberView.number = number;
    [self addGiftNumberFrom:number];
}
- (NSString *)getUserName{
    return self.nameLabel.text;
}
- (void)addGiftNumberFrom:(NSInteger)number{
    if (!self.isSetNumber) {
        self.numberView.number = number;
        self.isSetNumber = YES;
    }
    NSInteger num = self.numberView.number;
    [self.numberView changeNumber:num];
    [self handleNumber:num];
    self.model.currentNumber = num;
    self.creatDate = [NSDate date];
}
- (void)changeGiftNumber:(NSInteger)number{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.numberView changeNumber:number];
        [self handleNumber:number];
    });
}
#pragma mark - Private
- (void)handleNumber:(NSInteger )number{
    self.liveTimerForSecond = 0;
    NSString * numStr = [NSString stringWithFormat:@"%zi",number];
    CGFloat giftRight = numStr.length * kGiftNumberWidth + kGiftNumberWidth;
    [self.giftIV mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).offset(-kGiftNumberWidth - giftRight);
    }];
    if (numStr.length >= 4) {
        [self.giftIV mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.mas_right).offset(-kGiftNumberWidth * 6);
        }];
    }
    if (!CGAffineTransformIsIdentity(self.numberView.transform)) {
        [self.numberView.layer removeAllAnimations];
    }
    self.numberView.transform = CGAffineTransformIdentity;
    [UIView animateWithDuration:self.kNumberAnimationTime animations:^{
        self.numberView.transform = CGAffineTransformMakeScale(1.5, 1.5);
    } completion:^(BOOL finished) {
        if (finished) {
            self.numberView.transform = CGAffineTransformIdentity;
        }
    }];
    [self.liveTimer setFireDate:[NSDate date]];
}
- (void)liveTimerRunning{
    self.liveTimerForSecond += 1;
    if (self.liveTimerForSecond > self.kTimeOut) {
        if (self.isAnimation == YES) {
            self.isAnimation = NO;
            return;
        }
        self.isAnimation = YES;
        self.isLeavingAnimation = YES;
        CGFloat xChanged = [UIScreen mainScreen].bounds.size.width;
        switch (self.hiddenModel) {
            case LiveGiftHiddenModeLeft:
                xChanged = -xChanged;
                break;
            default:
                break;
        }
        if (self.hiddenModel == LiveGiftHiddenModeNone) {
            self.isLeavingAnimation = NO;
            if (self.liveGiftShowViewTimeOut) {
                self.liveGiftShowViewTimeOut(self);
            }
            [self removeFromSuperview];
        } else {
            [UIView animateWithDuration:self.kRemoveAnimationTime delay:self.kNumberAnimationTime options:UIViewAnimationOptionCurveEaseIn animations:^{
                self.transform = CGAffineTransformTranslate(self.transform, xChanged, 0);
            } completion:^(BOOL finished) {
                if (finished) {
                    self.isLeavingAnimation = NO;
                    if (self.liveGiftShowViewTimeOut) {
                        self.liveGiftShowViewTimeOut(self);
                    }
                    [self removeFromSuperview];
                }
            }];
        }
        [self stopTimer];
    }
}
- (void)setupContentContraints{
    [self.backIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0);
    }];
    [self.iconIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@6);
        make.width.height.equalTo(@30);
        make.centerY.equalTo(self);
    }];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(9);
        make.left.equalTo(self.iconIV.mas_right).offset(6);
        make.width.equalTo(@86);
    }];
    [self.sendLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_bottom).offset(-9);
        make.left.equalTo(self.nameLabel);
    }];
    [self.giftIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel.mas_right).offset(5).priority(750);
        make.width.equalTo(@32);
        make.height.equalTo(@24);
        make.centerY.equalTo(self);
    }];
    [self.numberView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).offset(-kGiftNumberWidth);
        make.centerY.height.equalTo(self);
    }];
}
- (UIImageView *)backIV{
    if (!_backIV) {
        _backIV = [self creatIV];
        _backIV.image = [UIImage imageNamed:@"w_liveGiftBack"];
    }
    return _backIV;
}
- (UIImageView *)iconIV{
    if (!_iconIV) {
        _iconIV = [self creatIV];
        _iconIV.image = [UIImage imageNamed:@"LiveDefaultIcon"];
        _iconIV.layer.cornerRadius = 15;
        _iconIV.layer.masksToBounds = YES;
    }
    return _iconIV;
}
- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [self creatLabel];
        _nameLabel.textColor = kNameLabelTextColor;
        _nameLabel.font = [UIFont systemFontOfSize:kNameLabelFont];
    }
    return _nameLabel;
}
- (UILabel *)sendLabel{
    if (!_sendLabel) {
        _sendLabel = [self creatLabel];
        _sendLabel.font = [UIFont systemFontOfSize:kGiftLabelFont];
        _sendLabel.textColor = kGiftLabelTextColor;
    }
    return _sendLabel;
}
- (UIImageView *)giftIV{
    if (!_giftIV) {
        _giftIV = [self creatIV];
    }
    return _giftIV;
}
- (LiveGiftShowNumberView *)numberView{
    if (!_numberView) {
        LiveGiftShowNumberView * nv = [[LiveGiftShowNumberView alloc]init];
        [self addSubview:nv];
        _numberView = nv;
    }
    return _numberView;
}
- (UIImageView *)creatIV{
    UIImageView * iv = [[UIImageView alloc]init];
    [self addSubview:iv];
    return iv;
}
- (UILabel * )creatLabel{
    UILabel * label = [[UILabel alloc]init];
    [self addSubview:label];
    return label;
}
- (NSTimer *)liveTimer{
    if (!_liveTimer) {
        _liveTimer =  [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(liveTimerRunning) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:_liveTimer forMode:NSRunLoopCommonModes];
    }
    return _liveTimer;
}
- (void)stopTimer{
    if (_liveTimer) {
        [_liveTimer invalidate];
        _liveTimer = nil;
    }
}
@end
